package com.ymatou.auth.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.auth.parameter.UserRegisterRequestDtoBean;
import com.ymatou.auth.service.UserRegisterRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*用户注册接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UserRegisterRequestDto{
	private static UserRegisterRequestDtoBean userregisterrequestdtoBean;
	private static UserRegisterRequestDtoCall userregisterrequestdtoCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户注册接口");
	}

	@Before
	public void caseUp() {
		userregisterrequestdtoBean=new UserRegisterRequestDtoBean();
		userregisterrequestdtoCall=new UserRegisterRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(userregisterrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_001() {
		Logger.start(true,"注册用户成功");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "F47C010581713BD523BE9B43677C4872");
			userData.put("sLoginId", TestDataManager.getRandomNum(7));
			userData.put("sMobile", TestDataManager.getRandomNum(12));
			userData.put("sTradingPassword", null);
			userData.put("bYmtLogin", true);
			userData.put("sLoginEmail", TestDataManager.getEmail());
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();			
			
			Logger.verifyEquals(true, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("ok", userregisterrequestdtoCall.getResult(), "验证结果");
			ULC201406Bean user = findUserByUid(uId);
			Logger.verifyNotNull(user, "验证user存在数据库中");
			Logger.verifyNotNull(user.getlPas(), "验证登录密码");
			Logger.verifyEquals(((String)userData.get("sLoginEmail")).toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(userData.get("sLoginId"), user.getlBills().getLoginId(), "验证登陆Id");
			Logger.verifyIsNull(user.gettPas(), "验证交易密码");
			
			Logger.comment("恢复数据，删除注册账号");
			deleteUserByUserId(uId);
			Logger.verifyIsNull(findUserByUid(uId), "验证数据已删除");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_002() {
		Logger.start(false,"不输入loginId,注册失败");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			uId = findUserByUid(uId)==null? uId:uId+1;
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "F47C010581713BD523BE9B43677C4872");
			userData.put("sMobile", TestDataManager.getRandomNum(12));
			userData.put("sTradingPassword", null);
			userData.put("sLoginEmail", TestDataManager.getEmail());
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();			
			
			Logger.verifyEquals(false, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals(true, userregisterrequestdtoCall.getResult().contains("创建用户凭据失败"), "验证结果");
			Logger.verifyIsNull(findUserByUid(uId), "验证user不在数据库中");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_003() {
		Logger.start(true,"不输入Mobile,注册成功");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "F47C010581713BD523BE9B43677C4872");
			userData.put("sLoginId", TestDataManager.getRandomNum(7));
			userData.put("sTradingPassword", "54E12B6B392FD98136162FE3F7343AD5");
			userData.put("sLoginEmail",TestDataManager.getRandomNum(11));
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();			
			
			Logger.verifyEquals(true, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("ok", userregisterrequestdtoCall.getResult(), "验证结果");
			ULC201406Bean user = findUserByUid(uId);
			Logger.verifyNotNull(user, "验证user存在数据库中");
			Logger.verifyNotNull(user.getlPas(), "验证登录密码");
			Logger.verifyEquals(((String)userData.get("sLoginEmail")).toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(userData.get("sLoginId"), user.getlBills().getLoginId(), "验证登陆Id");
			Logger.verifyNotNull(user.gettPas(), "验证交易密码");
			
			Logger.comment("恢复数据，删除注册账号");
			deleteUserByUserId(uId);
			Logger.verifyIsNull(findUserByUid(uId), "验证数据已删除");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_004() {
		Logger.start(true,"不输入Email,注册成功");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "F47C010581713BD523BE9B43677C4872");
			userData.put("sLoginId", TestDataManager.getRandomNum(7));
			userData.put("sMobile", TestDataManager.getRandomNum(12));
			userData.put("sTradingPassword", null);
			userData.put("sLoginEmail", TestDataManager.getEmail());
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();			
			
			Logger.verifyEquals(true, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("ok", userregisterrequestdtoCall.getResult(), "验证结果");
			ULC201406Bean user = findUserByUid(uId);
			Logger.verifyNotNull(user, "验证user存在数据库中");
			Logger.verifyNotNull(user.getlPas(), "验证登录密码");
			Logger.verifyEquals(((String)userData.get("sLoginEmail")).toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(userData.get("sLoginId"), user.getlBills().getLoginId(), "验证登陆Id");
			Logger.verifyIsNull(user.gettPas(), "验证交易密码");
			
			Logger.comment("恢复数据，删除注册账号");
			deleteUserByUserId(uId);
			Logger.verifyIsNull(findUserByUid(uId), "验证数据已删除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_005() {
		Logger.start(false,"输入空的密码,注册失败");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "");
			userData.put("sLoginId", TestDataManager.getRandomNum(7));
			userData.put("sMobile", TestDataManager.getRandomNum(12));
			userData.put("sTradingPassword", null);
			userData.put("sLoginEmail", TestDataManager.getEmail());
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();			
			
			Logger.verifyEquals(false, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals(true, userregisterrequestdtoCall.getResult().contains("登录密码不能为空"), "验证结果");
			Logger.verifyIsNull(findUserByUid(uId), "验证user不在数据库中");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_006() {
		Logger.start(false,"输入已使用过的loginId,注册失败");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "F47C010581713BD523BE9B43677C4872");
			userData.put("sLoginId", "testp49py3");
			userData.put("sMobile", TestDataManager.getRandomNum(12));
			userData.put("sTradingPassword", null);
			userData.put("sLoginEmail", TestDataManager.getEmail());
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();			
			
			Logger.verifyEquals(false, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals(ECode.AUTH_USER_EXIST, userregisterrequestdtoCall.getResult(), "验证结果");
			Logger.verifyIsNull(findUserByUid(uId), "验证user不在数据库中");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_007() {
		Logger.start(false,"输入已使用过的Email,注册失败");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "F47C010581713BD523BE9B43677C4872");
			userData.put("sLoginId", TestDataManager.getRandomNum(7));
			userData.put("sMobile", TestDataManager.getRandomNum(12));
			userData.put("sTradingPassword", null);
			userData.put("sLoginEmail", "ruanyifei@ymatou.com");
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();			
			
			Logger.verifyEquals(false, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals(ECode.AUTH_EMAIL_EXIST, userregisterrequestdtoCall.getResult(), "验证结果");
			Logger.verifyIsNull(findUserByUid(uId), "验证user不在数据库中");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserRegisterRequestDto_008() {
		Logger.start(false,"输入已使用过的Mobile,注册失败");
		try {
			Map<String, Object> userData = new HashMap<String, Object>();
			int uId = getNewUid();
			userData.put("iUserId", uId);
			userData.put("sType", "dms");
			userData.put("sPassword", "F47C010581713BD523BE9B43677C4872");
			userData.put("sLoginId", TestDataManager.getRandomNum(7));
			userData.put("sMobile", "13403102432");
			userData.put("sTradingPassword", null);
			userData.put("sLoginEmail", TestDataManager.getEmail());
			userregisterrequestdtoBean.setUData(userData);
			userregisterrequestdtoCall.setData(userregisterrequestdtoBean);
			userregisterrequestdtoCall.callService();
			//重复调用2次确保必定是存在重复数据
			userregisterrequestdtoCall.callService();	
			Logger.verifyEquals(false, userregisterrequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals(ECode.AUTH_MOBILE_EXIST, userregisterrequestdtoCall.getResult(), "验证结果");
			Logger.verifyIsNull(findUserByUid(uId), "验证user不在数据库中");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 生成新的UID
	 * @return
	 * @throws Exception
	 */
	private int getNewUid() throws Exception{
		int uId = Integer.valueOf(TestDataManager.getRandomNum(8));
		while(findUserByUid(uId)!=null){
			uId = Integer.valueOf(TestDataManager.getRandomNum(8));
		}
		return uId;
	}
	
	/**
	 * 根据UserID删除注册用户
	 * @param loginId
	 * @throws Exception 
	 */
	private void deleteUserByUserId(int uid) throws Exception{
		ULC201406Wapper wapper = new ULC201406Wapper();
		wapper.deleteData("{\"uId\":" + uid + "}");
	}
	
	/**
	 * 判断用户是否存在
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	private ULC201406Bean findUserByUid(int uid) throws Exception{
		ULC201406Wapper wapper = new ULC201406Wapper();
		ULC201406Bean bean = wapper.getULCInfoByUid(uid);
		return bean;
	}
	
}